//
//  BDFilePathTool.m
//  AVPlayerTest
//
//  Created by feng on 16/9/21.
//  Copyright © 2016年 冯宝东. All rights reserved.
//
#define  BD_DirHome     NSHomeDirectory()
#define  BD_DocumentDir [BD_DirHome stringByAppendingPathComponent:@"Documents"]
#define  BD_MySelfDir   [BD_DocumentDir stringByAppendingPathComponent:@"fengbaodong"]

#import "BDFilePathTool.h"
static BDFilePathTool *sigleTan;
@implementation BDFilePathTool

-(instancetype)init
{
    self=[super init];
    if (self) {
//        [self testString];
    }
    return self;
}
/**
 单例的快速创建
 
 @return 工具类单例
 */
+(instancetype)defaultTools
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sigleTan=[[self alloc] init];
    });
    return sigleTan;
}

/**
 判断是不是一个目录
 
 @param filePath 文件路径
 
 @return 是不是一个目录
 */
//-(BOOL)isDir:(NSString*)filePath
//{
//    BOOL isDir;
//    NSFileManager* fm=[NSFileManager defaultManager];
//    // 2) 判断是否是一个目录
//    [fm fileExistsAtPath:filePath isDirectory:&isDir];
//    
//    if (isDir) {
//        NSLog(@"这是一个目录");
//    }else{
//        
//        NSLog(@"这不是一个目录");
//    }
//    return isDir;
//}

/**
 获取相对Documents文件夹路径
 
 @param lastCompent 最后的文件夹名称
 
 @return 相对的绝对路径
 */
-(NSString*)getFilePathOrCreateFilePathWithlastCompentName:(NSString*)lastCompent
{
    NSString* filePath=[BD_DocumentDir stringByAppendingPathComponent:lastCompent];
    NSFileManager* fileManager=[NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath])
    {
        return filePath;
    }else
    {
        [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
    }

    return filePath;

}

/**
 获取或者创建文件夹全路径
 @param fullFilePath 文件夹全路径
 @return 绝对路径
 */
-(NSString*)getFilePathOrCreateFullFilePath:(NSString*)fullFilePath
{

    NSFileManager* fileManager=[NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:fullFilePath])
    {
        
    }else
    {
        [fileManager createDirectoryAtPath:fullFilePath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    return fullFilePath;

}

/**
 获取除了扩展名的路径
 
 @param filePath 文件路径
 
 @return 除了扩展名的路径
 */
-(NSString*)getExpctExtensionWithFilePath:(NSString*)filePath
{

    NSString*resultPath=[filePath stringByDeletingPathExtension];
    return resultPath;

}

/**
 获取文件路径的father路径
 
 @param filePath 文件路径
 
 @return 文件的父亲路径
 */
-(NSString*)getFatherPathWithFilePath:(NSString*)filePath
{
    NSString*resultPath=[filePath stringByDeletingLastPathComponent];
    return resultPath;
}

/**
 获取文件的说明
 
 @param filePath 文件路径
 
 @return 文件的说明
 */
-(NSDictionary*)getFileExplanationMessageWithFilePath:(NSString*)filePath
{

    NSFileManager* fileManager=[NSFileManager defaultManager];
    NSError* ERROR;
    
    NSDictionary* messageDic=[fileManager attributesOfItemAtPath:filePath error:&ERROR];
    
    return messageDic;
    
}
/**
 根据数据来创建文件
 
 @param resultData 数据
 @param name       文件名称
 */
-(void)createFileWithData:(NSData*)resultData withName:(NSString*)name
{
    NSFileManager* fileManager=[NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:BD_MySelfDir])
    {
        [fileManager createDirectoryAtPath:BD_MySelfDir withIntermediateDirectories:YES attributes:nil error:nil];
    }
    NSString *filePath=[BD_MySelfDir stringByAppendingPathComponent:name];
    BOOL createStatus= [fileManager createFileAtPath:filePath contents:resultData attributes:nil];
    
    if (createStatus) {
        NSLog(@"恭喜您文件创建成功！");
    }
    
}
//拷贝
-(BOOL)copyFileSourcePath:(NSString*)sourcePath toPath:(NSString*)toPath
{
    BOOL isResut=NO;
    NSError* error=nil;
    NSFileManager* fileManager=[NSFileManager defaultManager];
    isResut= [fileManager copyItemAtPath:sourcePath toPath:toPath error:&error];
    if (error) {
        NSLog(@"copy error log is %@",error.localizedDescription);
    }
    
    
    return isResut;
}


//移动
-(BOOL)moveFileSourcePath:(NSString*)sourcePath toPath:(NSString*)toPath
{
    BOOL isResut=NO;
    NSError* error=nil;
    NSFileManager* fileManager=[NSFileManager defaultManager];
    isResut= [fileManager moveItemAtPath:sourcePath toPath:toPath error:&error];
    if (error) {
        NSLog(@"move error log is %@",error.localizedDescription);
    }
    
    
    return isResut;

}


//删除
-(BOOL)removeFileSourcePath:(NSString*)sourcePath
{
    BOOL isResut=NO;
    
    
    NSError* error=nil;
    NSFileManager* fileManager=[NSFileManager defaultManager];
    isResut= [fileManager removeItemAtPath:sourcePath error:&error];
    if (error) {
        NSLog(@"remove error log is %@",error.localizedDescription);
    }

    return isResut;
    
}







+(float)fileSizeAtPath:(NSString *)path;
{
    
    NSFileManager *fileManager=[NSFileManager defaultManager];
    
    if([fileManager fileExistsAtPath:path]){
        
        long long size=[fileManager attributesOfItemAtPath:path error:nil].fileSize;
        
        return size/1000.0/1000.0;
        
    }
    
    return 0;
    
}

//计算文件夹里面文件大小 单位为兆
+(float)getDirSizeAtPath:(NSString *)path
{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    
    float folderSize;
    
    if ([fileManager fileExistsAtPath:path]) {
        
        NSArray *childerFiles=[fileManager subpathsAtPath:path];
        
        for (NSString *fileName in childerFiles) {
            
            NSString *absolutePath=[path stringByAppendingPathComponent:fileName];
            
            folderSize +=[self fileSizeAtPath:absolutePath];
            
        }
        
        return folderSize;
        
    }
    
    return 0;
    

}
+(void)clearCache:(NSString *)path{
    
    NSFileManager *fileManager=[NSFileManager defaultManager];
    
    if ([fileManager fileExistsAtPath:path]) {
        
        NSArray *childerFiles=[fileManager subpathsAtPath:path];
        
        for (NSString *fileName in childerFiles) {
            
            //如有需要，加入条件，过滤掉不想删除的文件
            
            NSString *absolutePath=[path stringByAppendingPathComponent:fileName];
            
            [fileManager removeItemAtPath:absolutePath error:nil];
            
        }
        
    }
    
//    [[SDImageCache sharedImageCache] cleanDisk];
    
}

-(void)testString
{
    
    NSFileManager* fileManager=[NSFileManager defaultManager];
    //    NSLog(@"BD_DirHome:%@",BD_DirHome);
    //    NSString* home1=[BD_DirHome stringByAppendingString:@"fengbaodong"];
    NSString* home2=BD_MySelfDir;
    //    NSLog(@"%@ \n%@",home1,home2);
    NSString* lastComp= [home2 stringByAppendingPathExtension:@"mp3"];
    
    
    
    
    if ([fileManager fileExistsAtPath:@",,,g/Desktop"])
    {
        NSLog(@"DOCUMENTS :%@",home2);
    }else
    {
        [fileManager createDirectoryAtPath:home2 withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    
    NSDictionary* msgDic=[self getFileExplanationMessageWithFilePath:lastComp];
    NSLog(@"说明： %@",msgDic);
    
    
    
    NSString*love=@"*love*love*love*love*love*love*love*love*love*love*love_______FENG";
    NSData *rDATA=[love dataUsingEncoding:NSUTF8StringEncoding];
    [self createFileWithData:rDATA withName:@"BAOLOVE.txt"];
    
    NSString*mp3Path=[[NSBundle mainBundle] pathForResource:@"testM" ofType:@"mp3"];
    NSData * mp3DATA=[NSData dataWithContentsOfFile:mp3Path];
    [self createFileWithData:mp3DATA withName:@"我要测试.txt"];
    
    
    //   BOOL copyResult= [self  copyFileSourcePath:mp3Path toPath:[BD_DocumentDir stringByAppendingPathComponent:@"我_想_想.mp3"]];
    //    NSLog(@"COPY RESULT IS %d",copyResult);
    
    
    //    BOOL copyResult= [self  moveFileSourcePath:[BD_DocumentDir stringByAppendingPathComponent:@"我_想_想——MoveFile.mp3"] toPath:[BD_MySelfDir stringByAppendingPathComponent:@"我_想_想——MoveFile.mp3"] ];
    //    NSLog(@"COPY RESULT IS %d",copyResult);
    //
//    BOOL copyResult= [self  removeFileSourcePath:[BD_MySelfDir stringByAppendingPathComponent:@"我要测试.mp3"]];
//    NSLog(@"COPY RESULT IS %d",copyResult);

    NSFileHandle *fileInHandle,*fileOutHandle;
    NSString* myMp3FilePath=[BD_DocumentDir stringByAppendingPathComponent:@"fengbaodong.mp3"];
    NSString* myTxtFilePath=[BD_MySelfDir stringByAppendingPathComponent:@"good.mp3"];
    NSLog(@"myMp3FilePath %@",myMp3FilePath);
    fileInHandle=[NSFileHandle fileHandleForReadingAtPath:myMp3FilePath];
    fileOutHandle=[NSFileHandle fileHandleForWritingAtPath:myTxtFilePath];
    NSData* mySongData=[NSData dataWithContentsOfFile:myMp3FilePath];
    NSData * readData=[fileInHandle readDataOfLength:mySongData.length/2.0];
    NSInteger SIZE=[fileInHandle seekToEndOfFile];
    NSLog(@"mySongData length %ld   handle size is %ld",mySongData.length,(long)SIZE);
    [fileOutHandle writeData:readData];
    [fileOutHandle closeFile];
    [fileInHandle closeFile];
    
    
//    [self createFileWithData:nil withName:@"good.txt"];
    //验证文件的内容是否写入
//    NSLog(@"%@",[NSData dataWithContentsOfFile:myTxtFilePath]);




    
}

@end
